﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace EAIClassLibrary
{
    public class MessageFactory
    {

        /// <summary>
        /// Receive a message, and initial its other element, such as msgid, corrid etc.
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <returns></returns>
        public static XmlDocument AddESBHeader(XmlDocument sourceXml)
        {
            var esbEnvelope = ESBEnvelopeEntity.FromXML(sourceXml);

            esbEnvelope.ESBHdVer = "20120608_1.1";
            esbEnvelope.SrvDate = DateTime.Now.ToString("yyyy-MM-dd");
            esbEnvelope.SrvTime = DateTime.Now.ToString("hh:mm:ss.ms");
            esbEnvelope.SrvOpVer = "20120606_1.1";
            esbEnvelope.MsgID = Guid.NewGuid().ToString();
            esbEnvelope.CorrID = Guid.NewGuid().ToString();
            esbEnvelope.ESBRspCode = "0";
            esbEnvelope.ESBRspDec = "Success";

            return esbEnvelope.ToXml();
        }

        /// <summary>
        /// Add esbheader for LA
        /// </summary>
        /// <param name="laxml"></param>
        /// <returns></returns>
        public static XmlDocument AddESBHeaderForLA(XmlDocument laxml, XmlDocument requestXml)
        {
            var requestEnvelope = ESBEnvelopeEntity.FromXML(requestXml);
            var esbEnvelope = new ESBEnvelopeEntity();

            esbEnvelope.ESBHdVer = "20120608_1.1";
            esbEnvelope.SrvDate = DateTime.Now.ToString("yyyy-MM-dd");
            esbEnvelope.SrvTime = DateTime.Now.ToString("hh:mm:ss.ms");
            esbEnvelope.SrvOpVer = "20120606_1.1";
            esbEnvelope.SenderID = requestEnvelope.ReceiverID;
            esbEnvelope.ReceiverID = requestEnvelope.SenderID;
            esbEnvelope.SrvOpName = requestEnvelope.SrvOpName;
            esbEnvelope.MsgID = Guid.NewGuid().ToString();
            esbEnvelope.CorrID = requestEnvelope.CorrID;
            esbEnvelope.ESBRspCode = "0";
            esbEnvelope.ESBRspDec = "Success";

            esbEnvelope.MsgBody = laxml.OuterXml;

            return esbEnvelope.ToXml();
        }

        /// <summary>
        /// Modify esbHeader for AURA Service
        /// </summary>
        /// <param name="laxml"></param>
        /// <param name="requestXml"></param>
        /// <returns></returns>
        public static XmlDocument AddESBHeaderForAURA(XmlDocument auraXml, XmlDocument requestXml)
        {
            var requestEnvelope = ESBEnvelopeEntity.FromXML(requestXml);
            var esbEnvelope = ESBEnvelopeEntity.FromXML(auraXml);
            ModifyResponseHeader(requestXml, esbEnvelope);

            esbEnvelope.ESBHdVer = "20120608_1.1";
            esbEnvelope.SrvDate = DateTime.Now.ToString("yyyy-MM-dd");
            esbEnvelope.SrvTime = DateTime.Now.ToString("hh:mm:ss.ms");
            esbEnvelope.SrvOpVer = "20120606_1.1";
            esbEnvelope.MsgID = Guid.NewGuid().ToString();
            esbEnvelope.ESBRspCode = "0";
            esbEnvelope.ESBRspDec = "Success";
            return esbEnvelope.ToXml();
        }

        /// <summary>
        /// Create a result xmldocument.
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <returns></returns>
        public static XmlDocument CreateESBResult(XmlDocument sourceXml)
        {
            var esbEnvelope = ESBEnvelopeEntity.FromXML(sourceXml);

            return esbEnvelope.ToResultXml();
        }

        /// <summary>
        /// Get message body.
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <returns></returns>
        public static XmlDocument GetMsgBody(XmlDocument sourceXml)
        {
            var msgBody = ESBEnvelopeEntity.FromXML(sourceXml).MsgBody;

            var result = new XmlDocument();

            try
            {
                result.LoadXml(msgBody);
            }
            catch
            {
            }

            return result;
        }

        /// <summary>
        /// Create a unknowed exception message.
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static XmlDocument CreateCustomExceptionXML(string errorCode, string errorMessage, XmlDocument requestXml)
        {
            ESBEnvelopeEntity entity = ESBEnvelopeEntity.CreateWithHeader();

            ModifyResponseHeader(requestXml, entity);
            
            entity.ESBRspCode = errorCode;
            entity.ESBRspDec = errorMessage;
            return entity.ToResultXml();
        }

        /// <summary>
        /// Force transfer message.
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <returns></returns>
        public static XmlDocument ForceTransferMsg(XmlDocument sourceXml)
        {
            return sourceXml;
        }

        /// <summary>
        /// Modify response header, just like: CorrID, ReceiverID, SenderID, SrvOpName.
        /// </summary>
        /// <param name="requestXml"></param>
        /// <param name="entity"></param>
        private static void ModifyResponseHeader(XmlDocument requestXml, ESBEnvelopeEntity entity)
        {
            var requestEntity = ESBEnvelopeEntity.FromXML(requestXml);
            entity.CorrID = requestEntity.CorrID;
            entity.SenderID = requestEntity.ReceiverID;
            entity.ReceiverID = requestEntity.SenderID;
            entity.SrvOpName = requestEntity.SrvOpName;
        }

        

        /// <summary>
        /// Create a unknowed exception message.
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static XmlDocument CreateCustomExceptionXML(string errorCode, string errorMessage, XmlDocument requestXml, XmlDocument laRcvMsg)
        {
            ESBEnvelopeEntity entity = new ESBEnvelopeEntity();
            entity.ESBHdVer = "20120608_1.1";
            entity.SrvDate = DateTime.Now.ToString("yyyy-MM-dd");
            entity.SrvTime = DateTime.Now.ToString("hh:mm:ss.ms");
            entity.SrvOpVer = "20120606_1.1";
            entity.MsgID = Guid.NewGuid().ToString();
            entity.CorrID = Guid.NewGuid().ToString();
            entity.ESBRspCode = errorCode;
            entity.ESBRspDec = errorMessage;
            entity.MsgBody = laRcvMsg.OuterXml;
            return entity.ToResultXml();
        }

        /// <summary>
        /// Create Sample XML Message
        /// </summary>
        /// <returns></returns>
        public static XmlDocument CreateSampleXML()
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml("<ns0:ESBEnvelope xmlns:ns0=\"http://MetLifeEAI.EAISchema\">  <ESBHeader>    <ESBHdVer>ESBHdVer_0</ESBHdVer>    <SrvDate>1999-05-31</SrvDate>    <SrvTime>13:20:00.000-05:00</SrvTime>    <SenderID>SenderID_0</SenderID>    <ReceiverID>ReceiverID_0</ReceiverID>    <SrvOpName>SrvOpName_0</SrvOpName>    <SrvOpVer>SrvOpVer_0</SrvOpVer>    <MsgID>MsgID_0</MsgID>    <CorrID>CorrID_0</CorrID>    <ESBRspCode>ESBRspCode_0</ESBRspCode>    <ESBRspDec>ESBRspDec_0</ESBRspDec>    <ResField1>ResField1_0</ResField1>    <ResField2>ResField2_0</ResField2>    <ResField3>ResField3_0</ResField3>    <ResField4>ResField4_0</ResField4>    <ResField5>ResField5_0</ResField5>  </ESBHeader>    <MsgBody><Sample>I'm from EAI.</Sample></MsgBody></ns0:ESBEnvelope>");
            System.Diagnostics.EventLog.WriteEntry("MessageFactory XML", "<ns0:ESBEnvelope xmlns:ns0=\"http://MetLifeEAI.EAISchema\">  <ESBHeader>    <ESBHdVer>ESBHdVer_0</ESBHdVer>    <SrvDate>1999-05-31</SrvDate>    <SrvTime>13:20:00.000-05:00</SrvTime>    <SenderID>SenderID_0</SenderID>    <ReceiverID>ReceiverID_0</ReceiverID>    <SrvOpName>SrvOpName_0</SrvOpName>    <SrvOpVer>SrvOpVer_0</SrvOpVer>    <MsgID>MsgID_0</MsgID>    <CorrID>CorrID_0</CorrID>    <ESBRspCode>ESBRspCode_0</ESBRspCode>    <ESBRspDec>ESBRspDec_0</ESBRspDec>    <ResField1>ResField1_0</ResField1>    <ResField2>ResField2_0</ResField2>    <ResField3>ResField3_0</ResField3>    <ResField4>ResField4_0</ResField4>    <ResField5>ResField5_0</ResField5>  </ESBHeader>    <MsgBody><Sample>I'm from EAI.</Sample></MsgBody></ns0:ESBEnvelope>");
            return document;
        }

        /// <summary>
        /// Create Sample chinese XML Message
        /// </summary>
        /// <returns></returns>
        public static XmlDocument CreateSampleXMLWithCn()
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml("<ns0:ESBEnvelope xmlns:ns0=\"http://MetLifeEAI.EAISchema\">  <ESBHeader>    <ESBHdVer>ESBHdVer_0</ESBHdVer>    <SrvDate>1999-05-31</SrvDate>    <SrvTime>13:20:00.000-05:00</SrvTime>    <SenderID>SenderID_0</SenderID>    <ReceiverID>ReceiverID_0</ReceiverID>    <SrvOpName>SrvOpName_0</SrvOpName>    <SrvOpVer>SrvOpVer_0</SrvOpVer>    <MsgID>MsgID_0</MsgID>    <CorrID>CorrID_0</CorrID>    <ESBRspCode>ESBRspCode_0</ESBRspCode>    <ESBRspDec>成功</ESBRspDec>    <ResField1>ResField1_0</ResField1>    <ResField2>ResField2_0</ResField2>    <ResField3>ResField3_0</ResField3>    <ResField4>ResField4_0</ResField4>    <ResField5>ResField5_0</ResField5>  </ESBHeader>    <MsgBody><Sample>中文</Sample></MsgBody></ns0:ESBEnvelope>");
            
            return document;
        }

        /// <summary>
        /// Create sample XML Message
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <returns></returns>
        public static XmlDocument CreateSampleXML(XmlDocument sourceXml)
        {
            var entity = ESBEnvelopeEntity.FromXML(sourceXml);
            entity.MsgBody = "<Sample>I'm from EAI.</Sample>";
            return entity.ToXml();
        }
    }
}
